package com.yyk.yykywd.ui.work;

import android.os.Bundle;
import android.text.TextUtils;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;

import com.orhanobut.hawk.Hawk;
import com.orhanobut.logger.Logger;
import com.yyk.yykywd.BusinessPeopleInfoKt;
import com.yyk.yykywd.R;
import com.yyk.yykywd.base.BaseActivity;
import com.yyk.yykywd.bean.CenterCarInfo;
import com.yyk.yykywd.bean.WorkEditcardInfo;
import com.yyk.yykywd.config.Constant;
import com.yyk.yykywd.http.ApiService;
import com.yyk.yykywd.util.Md5Util2;
import com.yyk.yykywd.util.OnClickFlagUtil;
import com.yyk.yykywd.util.ToastUtil;

import java.util.Map;
import java.util.TreeMap;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import io.reactivex.Observer;
import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.disposables.Disposable;
import io.reactivex.schedulers.Schedulers;

/**
 * 修改名片
 */
public class ReviseCardActivity extends BaseActivity {


    @BindView(R.id.tv_topname)
    TextView tvTopname;
    @BindView(R.id.et_position)
    EditText etPosition;
    @BindView(R.id.et_phone)
    EditText etPhone;
    @BindView(R.id.et_wechat)
    EditText etWechat;
    @BindView(R.id.et_mail)
    EditText etMail;
    @BindView(R.id.bt_save)
    Button btSave;

    @Override
    public int getLayoutId() {
        return R.layout.activity_revise_card;
    }

    @Override
    public void initView() {
        initToolbar();
    }

    private void initToolbar() {
        setBackArrow();
        setTitle("修改名片信息");
    }

    @Override
    public void initData() {
        postCenterCar();
    }

    /**
     * 接口7：我的名片（ggk）
     */
    private void postCenterCar() {

        Map<String, String> map = new TreeMap<>();
        map.put("uid", BusinessPeopleInfoKt.getUid());
        map.put("sign", Md5Util2.createSign(map));
        ApiService.getInstance().api.postCenterCar(map)
                .subscribeOn(Schedulers.io())     //调度线程
                .observeOn(AndroidSchedulers.mainThread())
                .unsubscribeOn(Schedulers.io())   //取消订阅
                .subscribe(new Observer<CenterCarInfo>() {
                    @Override
                    public void onSubscribe(Disposable d) {

                    }

                    @Override
                    public void onNext(CenterCarInfo info) {
                        Logger.e("我的名片" + info.toString());
//                        ToastUtil.showShort(info.getMsg());
                        if (info.getCode() == 1) {
                            etPhone.setText(info.getData().getPhone());
                            etPosition.setText(info.getData().getPosition());
                            etWechat.setText(info.getData().getWechat());
                            etMail.setText(info.getData().getMail());
                        }
                    }

                    @Override
                    public void onError(Throwable e) {

                    }

                    @Override
                    public void onComplete() {

                    }
                });
    }

    /**
     * 接口6：修改名片（ggk）
     */
    private void postWorkEditcard(final boolean flag) {
        if (TextUtils.isEmpty(etPosition.getText())) {
            ToastUtil.showShort("职位不能为空，请认真填写");
            return;
        }
        if (TextUtils.isEmpty(etPhone.getText())) {
            ToastUtil.showShort("手机号不能为空，请认真填写");
            return;
        }
        //        if (TextUtils.isEmpty(etPosition.getText())){
//            ToastUtil.showShort("职位不能为空");
//            return;
//        }
//        if (TextUtils.isEmpty(etPosition.getText())){
//            ToastUtil.showShort("手机号不能为空");
//            return;
//        }
//        if (TextUtils.isEmpty(etPosition.getText())){
//            ToastUtil.showShort("微信号不能为空");
//            return;
//        }
//        if (TextUtils.isEmpty(etPosition.getText())){
//            ToastUtil.showShort("邮箱号不能为空");
//            return;
//        }
        Map<String, String> map = new TreeMap<>();
        map.put("uid", BusinessPeopleInfoKt.getUid());
        map.put("position", etPosition.getText().toString().trim() + "");
        map.put("phone", etPhone.getText().toString().trim() + ""); //手机
        map.put("wechat", etWechat.getText().toString().trim() + "");
        map.put("mail", etMail.getText().toString().trim() + "");
        map.put("sign", Md5Util2.createSign(map));
        ApiService.getInstance().api.postWorkEditcard(map)
                .subscribeOn(Schedulers.io())     //调度线程
                .observeOn(AndroidSchedulers.mainThread())
                .unsubscribeOn(Schedulers.io())   //取消订阅
                .subscribe(new Observer<WorkEditcardInfo>() {
                    @Override
                    public void onSubscribe(Disposable d) {

                    }

                    @Override
                    public void onNext(WorkEditcardInfo info) {
                        Logger.e("修改名片" + info.toString());
                        ToastUtil.showShort(info.getMsg());
                        if (info.getCode() == 1) {
                            finish();
                        }
                    }

                    @Override
                    public void onError(Throwable e) {

                    }

                    @Override
                    public void onComplete() {

                    }
                });
    }


    @OnClick(R.id.bt_save)
    public void onViewClicked() {
        if (OnClickFlagUtil.isFastClick()) {
            return;
        }
        postWorkEditcard(true);
    }
}
